package cn.tedu.zhyl.appointment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.tedu.zhyl.appointment.mapper.ReservationMapper;
import cn.tedu.zhyl.appointment.pojo.dto.ReservationDTO;
import cn.tedu.zhyl.appointment.pojo.dto.ReservationParam;
import cn.tedu.zhyl.appointment.pojo.entity.Reservation;
import cn.tedu.zhyl.appointment.pojo.vo.ReservationVO;
import cn.tedu.zhyl.appointment.pojo.vo.TimeCountVO;
import cn.tedu.zhyl.appointment.service.ReservationService;
import cn.tedu.zhyl.base.exception.ServiceException;
import cn.tedu.zhyl.base.menus.ReservationStatus;
import cn.tedu.zhyl.base.response.JsonResult;
import cn.tedu.zhyl.base.response.PageResponse;
import cn.tedu.zhyl.base.response.StatusCode;
import cn.tedu.zhyl.base.utils.AuthContext;
import cn.tedu.zhyl.system.mapper.SysUserMapper;
import cn.tedu.zhyl.system.pojo.entity.SysUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public void addReservation(ReservationDTO dto) {
        // todo 如果已经预约超过3次，则不需要在进行预约

        SysUser sysUser = AuthContext.get();
        Long id = sysUser.getId();

        // 否则，允许添加预约
        Reservation reservation = BeanUtil.copyProperties(dto, Reservation.class);
        reservation.setVisitor(String.valueOf(id));
        reservation.setStatus(ReservationStatus.PENDING.getCode());
        reservation.setCreateTime(LocalDateTime.now());
        reservation.setCreateBy(sysUser.getId());
        try {
            reservationMapper.insert(reservation);
        } catch (Exception e){
            throw new ServiceException(StatusCode.EXIST_RESERVATION);
        }

    }

    @Override
    public PageResponse<ReservationVO> findByPage(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        List<ReservationVO> list = reservationMapper.findByPage(status);
        return PageResponse.of(new PageInfo<>(list));
    }

    @Override
    public Integer getCancelledCount() {
        return reservationMapper.getCancelledCount();
    }

    @Override
    public List<TimeCountVO> getCountByTime(LocalDateTime time) {
        LocalDateTime end = time.plusHours(24);
        return reservationMapper.getCountByTime(time,end);
    }

    @Override
    public void cancelReservation(Long id) {
        reservationMapper.updateStatusById(id,ReservationStatus.CANCELED.getCode());
    }

    @Override
    public void updateReservationStatus() {
        reservationMapper.updateReservation(LocalDateTime.now());
    }

    @Override
    public List<ReservationVO> selectList(ReservationParam requestParam) {
        log.debug("开始查询预约信息Service:{}", requestParam);
        List<ReservationVO> reservationVO = this.reservationMapper.selectList(requestParam);
//        Iterator var3 = reservationVO.iterator();
//
//        while(var3.hasNext()) {
//            ReservationVO reservationVO1 = (ReservationVO)var3.next();
//            reservationVO1.setCreateByName(this.sysUserMapper.findNameById(reservationVO1.getCreateBy()));
//        }
        reservationVO.forEach(reservationVO1 -> reservationVO1.setCreateByName(sysUserMapper.findNameById(reservationVO1.getCreateBy())));
//        reservationVO.forEach(reservationVO1 -> reservationVO1.setVisitor(sysUserMapper.findNameById(reservationVO1.getVisitor())));
        log.debug("查询预约信息Service返回:{}", reservationVO);
        return reservationVO;
    }

}
